package com.example.controller;


import com.example.common.Result;
import com.example.entity.Category;
import com.example.service.CategoryService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryContoller {
    @Resource
    CategoryService categoryService;

    @PostMapping("/add")
    public Result add(@RequestBody Category category){ //@RequestBody: 表示将请求体中的json数据转换为Admin对象
        categoryService.add(category);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){ //@PathVariable: 表示从路径中获取参数
        categoryService.delete(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category){ //@RequestBody: 表示将请求体中的json数据转换为Admin对象
        categoryService.update(category);
        return Result.success();
    }

    @GetMapping("/selectAll")  //  接口的路径
    public Result selectAll(Category category){
        List<Category> categoryList = categoryService.selectAll(category);
        return Result.success(categoryList);

    }

    /*
     *   分页查询
     * pageNum: 当前页码
     * pageSize: 每页的个数
     * */
    @GetMapping("/selectPage")// 接口的路径
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             // @RequestParam(required = false) String name
                             Category category
    ) {

        PageInfo<Category> pageInfo = categoryService.selectPage(pageNum, pageSize, category);
        return Result.success(pageInfo); //返回的是分页的对象
    }

}
